package com.plugins.server;


import com.alibaba.fastjson.JSONObject;
import com.iclient.dao.DaoUtils;
import com.iclient.dao.model.SinaWbUser;
import com.iclient.dao.service.SinaWbUserService;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

/**
 * 
 * @Author  zziahui
 * @Descp   rabbitMq客户端工作线程
 * @Date    2015年5月30日12:13:07
 *
 */
public class MsgConsumer extends DefaultConsumer{
	
	/**
	 * 父类的构造方法
	 * @param channel
	 */
	public MsgConsumer(Channel channel) {
		super(channel);
	}

	/**
	 * 重写父类的消费方法
	 */
	@Override
	public void handleDelivery(String consumerTag, Envelope envelope,
			BasicProperties properties, byte[] body){
		Long deliveryTag = envelope.getDeliveryTag();
		MQServer server = MQServer.getServer();
		try{
			JSONObject obj = (JSONObject) JSONObject.parse(body);
			String tag = obj.getString("task");
			if("sina:search:user".equals(tag)){
				SinaWbUser user = JSONObject.parseObject(obj.toJSONString(), SinaWbUser.class);
				SinaWbUserService sinaWbUserService = (SinaWbUserService)DaoUtils.getBean("sinaWbUserService");
				sinaWbUserService.addSinaUser(user);
				System.out.println(user.getPageid());
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try{
				server.ackMessage(deliveryTag);
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}

	
	
}